package paint2.ui;

import paint2.ui.ventanas.VentanaPrincipal;
import paint2.modelo.Figura;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JPanel;

public class SuperficiePintable extends JPanel{

    private Set<Figura> figuras = new HashSet();
    private MouseListener listener;
    private VentanaPrincipal ventana;

    public SuperficiePintable(VentanaPrincipal ventana) {
        this.ventana = ventana;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Figura unaFigura : figuras) {
            unaFigura.pintar(g2);
        }
    }

    public void pintarUna(Figura figura) {
        this.figuras.add(figura);
        this.repaint();
    }

    public Set<Figura> obtenerFiguras () {
        return this.figuras;
    }

    public void borrarTodasLasFiguras () {
        this.figuras = new HashSet();
        this.repaint();
    }

    public void agregarListener(MouseListener listener) {
        siExisteListenerLoElimina();
        this.listener = listener;
        addMouseListener(this.listener);
    }

    private void siExisteListenerLoElimina() {
        if (existeListener()) {
            removeMouseListener(this.listener);
            this.listener = null;
            ventana.cambiarElEstadoA("ninguna figura seleccionada");
        }
    }

    private boolean existeListener() {
        return this.listener != null;
    }

}
